package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import android.view.View
import com.glory.cqaospda.R
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.LotInfoBean
import com.glory.cqaospda.viewmodel.EmptyPassingStationModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import kotlinx.android.synthetic.main.activity_emptypassingstation.*
import kotlinx.android.synthetic.main.activity_title.*

class EmptyPassingStationActivity : BaseActivity<EmptyPassingStationModel>(){

    private lateinit var lotBean: ArrayList<LotInfoBean>
    var lotID = ""
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@EmptyPassingStationActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    override fun initData() {

        btnEmptyPassingStation.setOnClickListener{
            if (Constant.isFastClick()) {
                intent.putExtra("type", "Shiftchange")
                intent.setClass(this, SkipDialogActivity::class.java)
                startActivityForResult(intent, 1)
            }
        }

        viewModel.defUI.callObserve.observe(this) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        viewModel.defUI.toastEvent.value = ToastMessageBean("空过站成功", true)
                        finish()
                    }
                }
            }else{
                Constant.okgoError = false
            }
        }

    }

    override fun initView(savedInstanceState: Bundle?) {

        tvTitle.text = "空过站"
        tvBaseSubmit.visibility = View.GONE
        tvUserName.text = Constant.UserName
        FreeApi_ActivityManager.getActivityManager().addActivity(this)
        lotID = intent.getStringExtra("lotID").toString()
        lotBean = intent.getSerializableExtra("lotList") as ArrayList<LotInfoBean>
        tvLotId.text = lotBean[0].mLotId
        tvPackage.text = lotBean[0].mFactoryPackage
        tvQTY.text = lotBean[0].mLotQty
        tvStep.text = lotBean[0].mStepName
        tvCustomerDevice.text = lotBean[0].mCustomerDevice

        val instance = MyApp.instance
        instance!!.handler = handler
    }

    override fun layoutId(): Int {
        return R.layout.activity_emptypassingstation
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                1 -> {
                    val extras = data!!.extras
                    val name = extras!!.getString("name").toString()
                    val pass = extras.getString("pass").toString()
                    val map = JsonObject()
                    map.addProperty("LotID", lotBean[0].mLotId)
                    map.addProperty("UserName", name)
                    map.addProperty("PassWord", pass)
                    viewModel.defaultPassSationEnd(map)
                }
                108 ->{
                    finish()
                }
            }
        }
    }
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }
}